layui.config({
    base: '/static/layui/'
}).extend({
    tableSelect: 'tableSelect',
    treetable : 'treetable-lay/treetable'
}).use(['form', 'layer', 'jquery', 'table', 'laydate', 'upload','tableSelect','treetable'], function () {
    const form = layui.form;
    const table = layui.table;
    const tableSelect = layui.tableSelect;
    const treeTable = layui.treetable;
    const $ = layui.jquery;

    form.render()

    table.render({
        elem: '#table'
        ,title: '列表'
        ,method: 'post'
        ,height: 'full-100'
        // ,url: 'base_material/baseList'
        ,page: true //开启分页
        ,limit: 20
        ,limits: [20, 30, 40, 50, 60, 70, 80, 90, 100]
        ,loading: true
        ,cols: [
            [
                {type: 'checkbox'},
                { field: 'order', title: '序号',align:'center',templet: function (data) {
                        return data.LAY_TABLE_INDEX + 1
                    }},
                { field: 'djMaterial', title: '东矩料号' ,width:150},
                { field: 'cusMaterial', title: '客户料号' ,width:150},
                { field: 'typeOfMachine', title: '品名(机种)' ,width:150},
            ]
        ]
        ,text: {
            none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
        }
        ,data: []
        ,toolbar: '#tableToolbar'
        ,defaultToolbar: ['filter']
    });

    //监听表格内 行事件
    table.on('row(table)', function(obj){
        var data_index = $(this).attr('data-index');//得到当前的tr的index
        $(".layui-table-body tr").attr({ "style": "background:#FFFFFF; color:#666666" });//其他tr恢复颜色
        $(".layui-table-body tr[data-index=" + data_index + "]").attr({ "style": "background:#999;color:#666666" });
        renderTable(obj.data.bomId,obj.data.opComponentId)
    });

    var renderTable = function(bomId,opComponentId){
        treeTable.render({
            treeColIndex: 2,
            treeSpid: 0,
            treeIdName: 'bomId',
            treePidName: 'parentBomId',
            treeDefaultClose: false,
            treeLinkage: false,
            elem: '#tree_table',
            url: 'base_material/listDetails',
            height: 'full-100',
            method: 'post',
            where: {
                bomId: bomId,
                allId: opComponentId,
                cusCode: $('#cusCode').val()
            },
            cols: [
                [
                    {type: 'checkbox'},
                    { field: 'order', title: '序号',align:'center',templet: function (data) {
                            return data.LAY_TABLE_INDEX + 1
                        }},
                    { field: 'djMaterial', title: '东矩料号',align:'left' ,width:150},
                    { field: 'cusMaterial', title: '客户料号' ,width:150},
                    { field: 'materialMrk', title: '料号描述' ,width:150},
                    { field: 'typeOfMachine', title: '品名(机种)' ,width:150},
                    { field: 'cusCode', title: '供应商编码' ,width:150},
                    { field: 'cusName', title: '供应商名称',width:150 },
                    { field: 'classType', title: '存货大类' },
                    { field: 'className', title: '存货大类名称' },
                    { field: 'baseQty', title: '单位用量' ,width:150}
                ]
            ],
            text: {
                none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
            }
        })
    };

    tableSelect.render({
        elem: '#cusCode',
        checkedKey: 'cusCode',
        searchPlaceholder: '企业编码',
        searchKey: 'cusCode',
        table: {
            url: 'manage/common/list/customer'
            ,method: 'post'
            ,cols: [
                [
                    { type: 'radio' },
                    { field: 'cusCode', title: '企业编码' },
                    { field: 'cusName', title: '企业名称' },
                ]
            ]
        },
        done: function (elem, data) {
            var NEWJSON = []
            layui.each(data.data, function (index, item) {
                NEWJSON.push(item.cusCode)
            })
            elem.val(NEWJSON.join(","))

            tableSelect.render({
                elem: '#material',
                checkedKey: 'invCode',
                searchPlaceholder: '料号编码',
                searchKey: 'invCode',
                table: {
                    url: 'manage/select/getMaterial',
                    where: {cusCode:NEWJSON.join(",")},
                    cols: [
                        [
                            { type: 'radio' },
                            { field: 'invCode',width: 150, title: '料号编码' },
                            { field: 'invName',width: 150, title: '料号名称' },
                            { field: 'cusCode',width: 100, title: '企业编码' },
                            { field: 'cusName',width: 150, title: '企业名称' },
                        ]
                    ]
                },
                done: function (elem, data) {
                    var NEWJSON = []
                    layui.each(data.data, function (index, item) {
                        NEWJSON.push(item.invCode)
                    })
                    elem.val(NEWJSON.join(","))
                }
            })
        }
    })

    table.on('toolbar(table)', function(obj){
        switch (obj.event) {
            case 'select':
                //保存 获取 table选中的数据
                //获取 treeTable的数据
                let checkTreeStatus = table.checkStatus("tree_table").data;
                let data = table.getData("tree_table");
                $.each(data,function (index,obj){
                    $.each(checkTreeStatus,function (index2,item){
                        if (obj.opComponentId === item.opComponentId){
                            obj.checked = true
                        }
                    })
                })
                let shadeIndex = top.layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.4});
                $.post("/base_material/saveBaseMaterial",{list:JSON.stringify(data)}, function (d) {
                    if (d.code == '200') {
                        top.layer.alert("新增成功", {icon: 1});
                        table.reload('table');
                        renderTable(0)
                    } else {
                        top.layer.msg("新增失败," + d.msg, {icon: 2});
                    }
                    top.layer.close(shadeIndex);
                },'json')
                break;
            case 'find':
                if($('#cusCode').val() == ''){
                    layer.msg("请选择供应商", {icon: 2});
                    return;
                }
                table.reload('table', {
                    url: 'base_material/baseList',
                    where: {
                        cusCode:$('#cusCode').val(),
                        invCode:$('#material').val(),
                        invDef:$('#cInvDefine9').val(),
                    },
                    page: {
                        curr: 1
                    }
                });
                break;
        }
    })
})